package com.bw.controller;


import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.core.util.IdUtil;
import com.bw.Result;
import com.bw.domain.Chengpin;
import com.bw.domain.Product;
import com.bw.service.ChengpinService;
import com.bw.service.ProductService;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.bw.upload.UploadService;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/chen")
public class ChenpinController {
    @Autowired
    ChengpinService chengpinService;


    @Autowired
    ProductService productService;

    @Autowired
    UploadService uploadService;

    @Autowired
    FileService fileService;


    @RequestMapping("getfile")
    public Result getfile(@ModelAttribute Chunk chunk)
    {
        fileService.postFileUpload(chunk);

        if(chunk.getChunkNumber().equals(chunk.getTotalChunks()))
        {
            FileResult fileResult = fileService.mergeFile(chunk);

            return Result.success(fileResult);


        }


        return Result.success();
    }


    @RequestMapping("gettu")
    public   Result  gettu(MultipartFile file) throws IOException {
        String s = uploadService.uploadFile(file);

        return Result.success(s);

    }

    @RequestMapping("/list")
    public Result getlist()
    {
        List<Chengpin> list = chengpinService.selectlist();

        return Result.success(list);


    }




    @RequestMapping("/getadd")
    public Result getchen(@RequestBody Chengpin chengpin)
    {


        boolean save = chengpinService.save(chengpin);

        return Result.success();

    }


    @RequestMapping("/getxlk")
    public Result getxlk()
    {
        List<Product> list = productService.list();
        return Result.success(list);

    }



    @RequestMapping("/getxuehua")
    public Result getxuehua()
    {

        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();

        return Result.success(snowflakeNextIdStr);





    }
}
